Accessibility

DRK Volume 4 Article

Understanding the Structure of Search Application


The current ActionScript best practices whitepaper from Macromedia (actionscript_standards.pdf, a PDF download) recommends placing all ActionScript code on a single frame of the main timeline. This makes it very simple to locate all the code within a movie.

However, the larger and more complex an application becomes, the less desirable this tactic is. This is especially true if the application consists of multiple sections or screens, each with their own associated classes. As the movie becomes more complex, the amount of code and its complexity on the main timeline becomes increasingly unmanageable.

Search Application demonstrates an alternate way to structure applications. This technique treats the Symbol Library as a class browser and places references to externalized ActionScript files inside the symbols with which they are associated. This technique works particularly well for applications that contain multiple sections, views, or screens.

Structuring the Application

Search Application's interface contains three screens or views controlled by a main class (SearchApplicationClass). You could say that the main class contains and controls the views.

This maps well to Macromedia Flash because you can use movie clips to represent different parts of your application. In this case, the main controller symbol (SearchApplicationSymbol) contains multiple view/screen symbols (see Figure 1).


Figure 1. The Symbol Library within the Search Application sample application

So what controls everything? Ideally, each symbol or section of the movie would control itself. That way you avoid spaghetti code and begin to really take advantage of object-oriented programming. Luckily, there is a way to associate classes within symbol instances, which allows each symbol to effectively control itself.

Everything Is an Object

Macromedia Flash MX introduced some features that make it possible to associate custom classes with symbols, which allows those symbols to be treated as instances of ActionScript classes. This makes everything within the application an ActionScript object, including individual screens, components, and the application itself:

  • ScreenApplicationClass is the controller for the entire application
  • AboutScreenClass is the class associated with the About screen
  • SearchScreenClass is the class associated with the Search screen
  • SettingsScreenClass is the class associated with the Settings screen

There are a number of other classes as well, but these ones represent the main views and the controller.

Each class is placed in its own external ActionScript file. If you look in the classes directory, you will see all the classes within the application. This means that the actual FLA contains no ActionScript at all; it contains only #include directives that point to the ActionScript files.

There are a number of advantages to externalizing the ActionScript code in this manner:

  • It allows you to integrate your project easily with source and version control systems. In fact, if you are using a system such as CVS, then multiple developers can work on the same file at the same time.
  • It allows developers to work on the code within the movie at the same time that designers are working on the design within the FLA.
  • It allows you to use the editing environment of your choice (Dreamweaver, HomeSite, BBEdit, EditPlus, and the like).
  • It makes it easier to reuse classes. This may or may not be an advantage depending on the functionality that the classes encapsulate.

Each of our main symbols has an ActionScript class associated with them. This association is made possible by Object.registerClass, which allows a symbol instance to be linked to an ActionScript class.

If you look in the classes/ScreenApplicationClass.as file, you will find the following line toward the bottom of the file.

Object.registerClass("SearchApplicationSymbol",SearchApplicationClass);

This tells Macromedia Flash that whenever the symbol with the linkage id of "SearchApplicationSymbol" appears in the movie, associate it with the ActionScript classed named "SearchApplicationClass."

In essence, this merges the symbol with the custom class so that assets within the symbol can be treated as properties of the class instance.

Navigating the Application

There are two steps that make the application easier to navigate and maintain.

First, we use a set-naming convention for symbols with associated classes. If a symbol has a class associated with it, it uses the following naming scheme:

[SymbolName]Symbol

which is associated with the following class:

[SymbolName]Class

The linkage ID is the same as the symbol name.

For example, SearchApplicationSymbol is associated with SearchApplicationClass. This makes it very easy to know which symbols have classes associated with them and which classes are associated with specific symbols.

Second, we structure the symbols within the library by grouping them logically according to their functionality and the class hierarchy they represent. This makes it simple to navigate quickly through the application, its assets, and classes.

Furthermore, because each main symbol is associated with an ActionScript class, the symbol library becomes a class browser. This allows you to find quickly where classes are located, as well as view the class hierarchy.